home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr47 / 335_04.zip / GETOPT.H < prev    next >
Text File  |  1993-04-13  |  3KB  |  108 lines

  1.  
  2.  
  3. /*
  4. HEADER:     ;
  5. TITLE:         Frankenstein Cross Assemblers;
  6. VERSION:     2.0;
  7. DESCRIPTION: "    Reconfigurable Cross-assembler producing Intel (TM)
  8.         Hex format object records.  ";
  9. SYSTEM:     UNIX, MS-Dos ;
  10. FILENAME:     getopt.h;
  11. WARNINGS:     "This is some ancient code I found on a version 7 system
  12.         when I was running the original port.  Asking for help from
  13.         the original authors is not advised.  (Especially after
  14.         the hack job I did on it.  Mark Zenier.)  "  ;
  15. SEE-ALSO:     frasmain.c;
  16. AUTHORS:     Keith Bostic, Rich $alz;
  17. */
  18. /*
  19. **  This is a public domain version of getopt(3).
  20. **  Bugs, fixes to:
  21. **        Keith Bostic
  22. **            ARPA: keith@seismo
  23. **            UUCP: seismo!keith
  24. **  Added NO_STDIO, opterr handling, Rich $alz (mirror!rs).
  25.  
  26.   Framework Cross Assembler 
  27.     use strchr
  28.     remove NO_STDIO code
  29.     Mark Zenier     Specialized Systems Consultants, Inc.   
  30. */
  31.  
  32. /*
  33. **  Error macro.  Maybe we want stdio, maybe we don't.
  34. **  The (undocumented?) variable opterr tells us whether or not
  35. **  to print errors.
  36. */
  37.  
  38. #define tell(s)                                \
  39.     if (opterr)                            \
  40.         (void)fputs(*nargv, stderr),                \
  41.         (void)fputs(s,stderr),                    \
  42.         (void)fputc(optopt, stderr),                \
  43.         (void)fputc('\n', stderr)
  44.  
  45.  
  46.  
  47. /* Global variables. */
  48. static char     EMSG[] = "";
  49. int         opterr = 1;        /* undocumented error-suppressor*/
  50. int         optind = 1;        /* index into argv vector    */
  51. int         optopt;        /* char checked for validity    */
  52. char        *optarg;        /* arg associated with option    */
  53.  
  54.  
  55. getopt(nargc, nargv, ostr)
  56.     int              nargc;
  57.     char        **nargv;
  58.     char         *ostr;
  59. {
  60.     static char         *place = EMSG;    /* option letter processing    */
  61.     register char     *oli;        /* option letter list index    */
  62.  
  63.     if (!*place)            /* update scanning pointer    */
  64.     {
  65.     if (optind >= nargc || *(place = nargv[optind]) != '-' || !*++place)
  66.         return(EOF);
  67.     if (*place == '-')        /* found "--"            */
  68.     {
  69.         optind++;
  70.         return(EOF);
  71.     }
  72.     }
  73.     /* option letter okay? */
  74.     if ((optopt = *place++) == ':' || (oli = strchr(ostr, optopt)) == NULL)
  75.     {
  76.     if (!*place)
  77.         optind++;
  78.     tell(": illegal option -- ");
  79.     goto Bad;
  80.     }
  81.     if (*++oli != ':')            /* don't need argument        */
  82.     {
  83.     optarg = NULL;
  84.     if (!*place)
  85.         optind++;
  86.     }
  87.     else                /* need an argument        */
  88.     {
  89.     if (*place)
  90.         optarg = place;        /* no white space        */
  91.     else
  92.         if (nargc <= ++optind)
  93.         {
  94.         place = EMSG;
  95.         tell(": option requires an argument -- ");
  96.         goto Bad;
  97.         }
  98.         else
  99.         optarg = nargv[optind];    /* white space            */
  100.     place = EMSG;
  101.     optind++;
  102.     }
  103.     return(optopt);            /* dump back option letter    */
  104. Bad:
  105.     return('?');
  106. }
  107.  
  108.